package com.cskaoyan.controller;


import com.cskaoyan.bean.Ad;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bo.SearchGrouponRulesBO;
import com.cskaoyan.bo.AdBO;
import com.cskaoyan.bo.CouponBO;
import com.cskaoyan.bo.CouponUserBo;
import com.cskaoyan.bo.SearchTopicBO;

import com.cskaoyan.service.ExtensionManagementService;
import com.cskaoyan.vo.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("admin")
@RequiresRoles(value = {"超级管理员", "推广管理员"}, logical = Logical.OR)
public class ExtensionManagementController {
    @Autowired
    ExtensionManagementService extensionManagementService;

    @GetMapping("ad/list")
    public BaseRespVo adList(AdBO adBO){
        AdVO adVO = extensionManagementService.query(adBO);
        return BaseRespVo.ok(adVO);
    }


    @PostMapping("ad/update")
    public BaseRespVo adUpdate(@RequestBody Ad ad){
        extensionManagementService.updateAd(ad);
        return BaseRespVo.ok();
    }

    @PostMapping("ad/create")
    public BaseRespVo adCreate(@RequestBody Ad ad){
        Ad ad1 = extensionManagementService.addAd(ad);
        return BaseRespVo.ok(ad1);
    }

    @PostMapping("ad/delete")
    public BaseRespVo adDelete(@RequestBody Ad ad) {
        extensionManagementService.deleteAd(ad);
        return BaseRespVo.ok();
    }

    @GetMapping("coupon/list")
    public BaseRespVo couponList(CouponBO couponBO){
        CouponVO couponVO = extensionManagementService.queryCoupon(couponBO);
        return BaseRespVo.ok(couponVO);
    }

    @PostMapping("coupon/delete")
    public BaseRespVo couponDelete(@RequestBody Coupon coupon){
        extensionManagementService.deleteCoupon(coupon);
        return BaseRespVo.ok();
    }

    @PostMapping("coupon/create")
    public BaseRespVo couponCreate(@RequestBody @Validated Coupon coupon){
        Coupon coupon1 = extensionManagementService.addCoupon(coupon);
        return BaseRespVo.ok(coupon1);
    }

    @PostMapping("coupon/update")
    public BaseRespVo couponUpdate(@RequestBody @Validated Coupon coupon){
        extensionManagementService.updateCoupon(coupon);
        return BaseRespVo.ok();
    }

    @GetMapping("coupon/read")
    public BaseRespVo couponRead(Integer id){
        Coupon coupon = extensionManagementService.getCoupon(id);
        return BaseRespVo.ok(coupon);
    }

    @GetMapping("coupon/listuser")
    public BaseRespVo couponListUser(CouponUserBo couponUserBo){
        CouponUserVO couponUserVO = extensionManagementService.queryCouponUser(couponUserBo);
        return BaseRespVo.ok(couponUserVO);
    }


    //获取专题管理列表
    @GetMapping("topic/list")
    public BaseRespVo topicList(SearchTopicBO searchTopicBO){
        TopicVO topicVO = extensionManagementService.getTopic(searchTopicBO);
        return BaseRespVo.ok(topicVO);
    }

    //增加专题(异常处理)
    @PostMapping("topic/create")
    public BaseRespVo createTopic(@RequestBody@Valid Topic topic){
        CreateTopicVO createTopicVO = extensionManagementService.createTopic(topic);
        return BaseRespVo.ok(createTopicVO);
    }

    //删除专题（逻辑删除）
    @PostMapping("topic/delete")
    public BaseRespVo deleteTopic(@RequestBody Topic topic){
        extensionManagementService.deleteTopic(topic);
        return BaseRespVo.ok();
    }

    //编辑专题
    @PostMapping("topic/update")
    public BaseRespVo updateTopic(@RequestBody@Valid Topic topic){
        extensionManagementService.updateTopic(topic);
        return BaseRespVo.ok(topic);
    }

    //获取团购规则列表
    @GetMapping("groupon/list")
    public BaseRespVo grouponRulesList(SearchGrouponRulesBO searchGrouponRulesBO){
        GrouponRulesVO grouponRulesVO = extensionManagementService.getGrouponRules(searchGrouponRulesBO);
        return BaseRespVo.ok(grouponRulesVO);
    }

    //删除团购规则（逻辑删除）
    @PostMapping("groupon/delete")
    public BaseRespVo deleteGrouponRules(@RequestBody GrouponRules grouponRules){
        extensionManagementService.deleteGrouponRules(grouponRules);
        return BaseRespVo.ok();
    }

    //新增团购规则
    @PostMapping("groupon/create")
    public BaseRespVo createGrouponRules(@RequestBody@Valid GrouponRules grouponRules){

        //判断商品Id必须存在
        Integer count = extensionManagementService.exitGoodsId(grouponRules);
        if(count == 0){
            return BaseRespVo.fail("商品ID不存在", 401);
        }

        GrouponRules newGrouponRules = extensionManagementService.createGrouponRules(grouponRules);
        return BaseRespVo.ok(newGrouponRules);
    }

    //编辑团购规则
    @PostMapping("groupon/update")
    public BaseRespVo updateGrouponRules(@RequestBody@Valid GrouponRules grouponRules){
        //判断商品Id必须存在
        Integer count = extensionManagementService.exitGoodsId(grouponRules);
        if(count == 0){
            return BaseRespVo.fail("商品ID不存在", 401);
        }

        extensionManagementService.updateGrouponRules(grouponRules);
        return BaseRespVo.ok();
    }

    //获取团购活动列表（复用SearchGrouponRulesBO）
    //TODO:响应体参数未知
    @GetMapping("groupon/listRecord")
    public BaseRespVo grouponRecordList(SearchGrouponRulesBO searchGrouponRulesBO){
        GrouponRecordVO grouponRecordVO = extensionManagementService.getGrouponRecord(searchGrouponRulesBO);
        return BaseRespVo.ok(grouponRecordVO);
    }
}
